package com.dropdb.demo.controller.admin;

import com.dropdb.demo.bean.*;
import com.dropdb.demo.bean.vo.*;
import com.dropdb.demo.service.AdminMarketService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Alone
 * @version 1.0
 * @date 2020/6/26 16:11
 */
@RestController
@RequestMapping("admin")
public class AdminMarketController {

    @Autowired
    AdminMarketService adminMarketService;

    /**
     * 查询行政区域
     * @return
     */
    @RequestMapping("region/list")
    public BaseRespVO regionList(){
        List<MarketRegionListVO> vos = adminMarketService.queryRegions();
        return BaseRespVO.ok(vos);
    }

    /**
     * 品牌制造商
     * @param page 当前页
     * @param limit 每页限制数量
     * @param sort 排序依据
     * @param order 排序方式
     * @return
     */
    @RequestMapping("brand/list")
    @RequiresPermissions(value = {"*", "admin:brand:list"}, logical = Logical.OR)
    public BaseRespVO brandList(Integer page, Integer limit, String sort, String order, Integer id, String name){
        MarketBaseDataVO marketBaseDataVO = adminMarketService.queryBrands(page, limit, sort, order,id,name);
        return BaseRespVO.ok(marketBaseDataVO);
    }

    @RequestMapping("brand/update")
    @RequiresPermissions(value = {"*", "admin:brand:update"}, logical = Logical.OR)
    public BaseRespVO brandUpdate(@RequestBody Brand brand){
        Brand vo=adminMarketService.updateBrand(brand);
        return BaseRespVO.ok(vo);
    }

    @RequestMapping("brand/delete")
    @RequiresPermissions(value = {"*", "admin:brand:delete"}, logical = Logical.OR)
    public BaseRespVO brandDelete(@RequestBody Brand brand){
        adminMarketService.deleteBrand(brand);
        return BaseRespVO.ok();
    }

    @RequestMapping("brand/create")
    @RequiresPermissions(value = {"*", "admin:brand:create"}, logical = Logical.OR)
    public BaseRespVO brandCreate(@RequestBody Brand bo){
        if(bo.getDesc()==null||bo.getFloorPrice()==null||bo.getName()==null){
            return BaseRespVO.error(401,"参数不对");
        }
        Brand vo=adminMarketService.createBrand(bo);
        return BaseRespVO.ok(vo);
    }

    /**
     * 订单管理
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @RequestMapping("order/list")
    @RequiresPermissions(value = {"*", "admin:order:list"}, logical = Logical.OR)
    public BaseRespVO orderList(Integer page, Integer limit, String sort, String order, Short[] orderStatusArray, Integer userId, String orderSn){
        MarketBaseDataVO marketBaseDataVO = adminMarketService.queryOrders(page, limit, sort, order,orderStatusArray,userId,orderSn);
        return BaseRespVO.ok(marketBaseDataVO);
    }

    @RequestMapping("order/detail")
    @RequiresPermissions(value = {"*", "admin:order:read"}, logical = Logical.OR)
    public BaseRespVO orderDetail(Integer id){
        MarketOrderDetailVO vo=adminMarketService.orderDetail(id);
        return BaseRespVO.ok(vo);
    }

    @RequestMapping("order/refund")
    @RequiresPermissions(value = {"*", "admin:order:refund"}, logical = Logical.OR)
    public BaseRespVO orderRefund(@RequestBody Map bo){
        Order vo= adminMarketService.orderRefund(bo);
        return BaseRespVO.ok(vo);
    }

    @RequestMapping("order/ship")
    @RequiresPermissions(value = {"*", "admin:order:ship"}, logical = Logical.OR)
    public BaseRespVO orderShip(@RequestBody Map bo){
        Order vo=adminMarketService.orderShip(bo);
        return BaseRespVO.ok(vo);
    }

    /**
     * 通用问题
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @RequestMapping("issue/list")
    @RequiresPermissions(value = {"*", "admin:issue:list"}, logical = Logical.OR)
    public BaseRespVO issueList(Integer page, Integer limit, String sort, String order, String question){
        MarketBaseDataVO marketBaseDataVO = adminMarketService.queryIssues(page, limit, sort, order,question);
        return BaseRespVO.ok(marketBaseDataVO);
    }

    @RequestMapping("issue/delete")
    @RequiresPermissions(value = {"*", "admin:issue:delete"}, logical = Logical.OR)
    public BaseRespVO issueDelete(@RequestBody Issue bo){
        adminMarketService.deleteIssue(bo);
        return BaseRespVO.ok();
    }
    @RequestMapping("issue/create")
    @RequiresPermissions(value = {"*", "admin:issue:create"}, logical = Logical.OR)
    public BaseRespVO issueCreate(@RequestBody Issue issue){
        Issue vo=adminMarketService.createIssue(issue);
        return BaseRespVO.ok(vo);
    }

    @RequestMapping("issue/update")
    @RequiresPermissions(value = {"*", "admin:issue:update"}, logical = Logical.OR)
    public BaseRespVO issueUpdate(@RequestBody Issue bo){
        Issue vo =adminMarketService.issueUpdate(bo);
        return BaseRespVO.ok(vo);
    }

    /**
     * 关键词
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @RequestMapping("keyword/list")
    @RequiresPermissions(value = {"*", "admin:keyword:list"}, logical = Logical.OR)
    public BaseRespVO keywordList(Integer page, Integer limit, String sort, String order, String keyword, String url){
        MarketBaseDataVO marketBaseDataVO = adminMarketService.queryKeywords(page, limit, sort, order,keyword,url);
        return BaseRespVO.ok(marketBaseDataVO);
    }

    @RequestMapping("keyword/create")
    @RequiresPermissions(value = {"*", "admin:keyword:create"}, logical = Logical.OR)
    public BaseRespVO keywordCreate(@RequestBody Keyword bo){
        if(bo.getUrl()==null){
            return BaseRespVO.error(401,"参数不对");
        }
        Keyword vo=adminMarketService.createKeyword(bo);
        return BaseRespVO.ok(vo);
    }

    @RequestMapping("keyword/update")
    @RequiresPermissions(value = {"*", "admin:keyword:update"}, logical = Logical.OR)
    public BaseRespVO keywordUpdate(@RequestBody Keyword bo){
        Keyword vo=adminMarketService.updateKeyword(bo);
        return BaseRespVO.ok(vo);
    }

    @RequestMapping("keyword/delete")
    @RequiresPermissions(value = {"*", "admin:keyword:delete"}, logical = Logical.OR)
    public BaseRespVO keywordDelete(@RequestBody Keyword bo){
        adminMarketService.deleteKeyword(bo);
        return BaseRespVO.ok();
    }

    /**
     * 商品类目
     * @return
     */
    @RequestMapping("category/list")
    @RequiresPermissions(value = {"*", "admin:category:list"}, logical = Logical.OR)
    public BaseRespVO categoryList(){
        List<MarketCategoryListVO> vos = adminMarketService.queryCategories();
        return BaseRespVO.ok(vos);
    }

    @RequestMapping("category/{level}")
    public BaseRespVO categoryLevel(@PathVariable("level")String level){
        List<MarketCategoryLevelVO> categories = adminMarketService.queryCategoryByLevel(level);
        return BaseRespVO.ok(categories);
    }

    @RequestMapping("category/update")
    @RequiresPermissions(value = {"*", "admin:category:update"}, logical = Logical.OR)
    public BaseRespVO categoryUpdate(@RequestBody Category bo){
        Category vo=adminMarketService.updateCategory(bo);
        return BaseRespVO.ok(vo);
    }

    @RequestMapping("category/create")
    @RequiresPermissions(value = {"*", "admin:category:create"}, logical = Logical.OR)
    public BaseRespVO categoryCreate(@RequestBody Category category){
        Category vo=adminMarketService.createCategory(category);
        return BaseRespVO.ok(vo);
    }
    @RequestMapping("category/delete")
    @RequiresPermissions(value = {"*", "admin:category:delete"}, logical = Logical.OR)
    public BaseRespVO categoryDelete(@RequestBody MarketCategoryListVO bo){
        adminMarketService.deleteCategory(bo);
        return BaseRespVO.ok();
    }

    /**
     * 文件上传
     * @param file
     * @param model
     * @return
     */
    /*@RequestMapping("storage/create")
    public BaseRespVo storageCreate(@RequestParam(value = "file") MultipartFile file, Model model){
        return null;
    }*/
}
